//
//  TopicListViewModel.swift
//  VO_Yuejie
//
//  Created by laichunhui on 15/11/18.
//  Copyright © 2015年 lch. All rights reserved.


import UIKit

//MARK: - 主题视图列表

class TopicListViewModel {
    
    /// 主题数据的数组懒加载
    var topics = [TopicViewModel]()
    
   /// 加载首页主题
    func loadLastestTopic(finished: (error: NSError?)->()) {
         // 首页界面count = 4，刷新最新的4条
        TopicDAL.loadTopics(4, offset: 0) { (array, error) -> () in
            
            // 1. 判断错误
            if error != nil {
                finished(error: error)
                return
            }
            
            // 2. 遍历数组，字典转模型
            var arrayM = [TopicViewModel]()
            
            for dict in array! {
                arrayM.append(TopicViewModel(topic: Topic(dict: dict)))
            }
            
            // 3. 刷新主题数组
            self.topics = arrayM
         //   print(self.topics)

            finished(error: nil)

        }
    }

    /// 加载主题数组
    /// - parameter refreshType: 刷新类型
    
    func loadTopics(refreshType: RefreshType, finished: (error: NSError?)->()) {
       
        // 往期界面count=10
        
        // 利用offset加载比最后一个主题早的主题
        var offset = 0
        
        if refreshType == RefreshType.PullUp {
            offset = topics.count
        }
        
        TopicDAL.loadTopics(5, offset: offset) { (array, error) -> () in

            // 1. 判断错误
            if error != nil {
                finished(error: error)
                return
            }
            // 2. 遍历数组，字典转模型
            // 1> 创建一个可变的主题模型数组
            var arrayM = [TopicViewModel]()
            
            // 2> 遍历
            for dict in array! {
                arrayM.append(TopicViewModel(topic: Topic(dict: dict)))
            }
            
            // 3> 拼接数组
            // 如果是上拉刷新，将数组追加到末尾
            if refreshType == RefreshType.PullUp {
                self.topics += arrayM
            } else {
                // 下拉刷新
                self.topics = arrayM
            }
            
            finished(error: nil)
        }
    }
}


//MARK: - 主题视图模型
/**
 对于没有`父类`的 class，如果要编写调试信息，需要遵守 CustomStringConvertible
 然后实现 var description: String
 */
class TopicViewModel: CustomStringConvertible {
    
    /// 主题模型
    var topic: Topic
    
    /// cell高度
    var rowHeight: CGFloat = ScreenWidth / 1.516 + 57 + 85
    
    /// 创建时间
    var createAt: String {
        if let date = NSDate.sinaDate(topic.created_at ?? "") {
            return date.dateDescription
        }
        return ""
    }
    
    /// 发布时间
    var publishAt: NSDate {
        if let date = NSDate.yuejieDate(topic.publish_at ?? "") {
            return date
        }
        return NSDate()
    }
    
    
    
    /// 作者昵称
    var userNick: String? {
        return topic.value_object?.user?.nick
    }
    /// 作者头像 URL
    var userIconUrl: NSURL {
        return NSURL(string: topic.value_object?.user?.icon_url ?? "")!
    }
    
    /// 配图 URL
    var image_url: NSURL {
        return NSURL(string: topic.image_url ?? "")!
    }
    
    /// 标题
    var title: String? {
        return topic.title
    }
    
    /// 简介
    var intro: String? {
        return topic.value_object?.intro
    }
    /// 简介内容高度
    lazy var introHeight: CGFloat = {
        let nsStr: NSString = NSString(string: self.topic.value_object!.intro!)
        let height: CGFloat = nsStr.boundingRectWithSize(CGSizeMake(ScreenWidth - 20, CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(12)], context: nil).size.height
        
        return height
    }()
    /// headerView高度
    lazy var headerViewHeight: CGFloat = ScreenWidth / 1.516 + 56 + self.introHeight
    
    /// 构造函数
    init(topic: Topic) {
        self.topic = topic
    }

    /// 描述信息
    var description: String {
        return topic.description
    }
}

